import { ElMessage } from 'element-plus'
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { encryptPwd, textEncrypt, textDecrypt } from '../api/comm.js'

/**
 * 登录hooks组合式函数
 * @returns
 */
export const useLogin = () => {
    const router = useRouter() // 路由对象
    // 挂载生命周期
    onMounted(() => {
        showUserInfo()
    })
    // 登录表单账户数据
    const account = ref({
        username: '',
        password: '',
        remmberPsw: false, // 记住密码
    })
    // 表单验证规则
    const rules = ref({
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
    })
    // 表单对象
    const loginRef = ref(null)
    // 登录表单提交事件
    const bindLogin = () => {
        loginRef.value.validate(valid => {
            if (valid) {
                onLogin()
            }
        })
    }
    // 登录业务
    const onLogin = async () => {
        // 登录接口
        const username = account.value.username
        const password = account.value.password
        if (username === '19808203127' && password === '123456') {
            ElMessage.success('登录成功') //登录成功
            const token = String(tokenValue(password, 2)) //生成token
            account.value.password = String(pwdEncrypt(password)) //加密密码
            onRemmberPassword() //记住密码
            localStorage.setItem('TOKEN', token)
            // router.push({ path: '/home' })
            window.location.href='/home'
        } else {
            ElMessage.error('账号密码错误') // 账户出错提示
        }
    }
    /**
     * 记住密码
     */
    const onRemmberPassword = () => {
        if (account.value.remmberPsw) {
            console.log('afe', account)
            localStorage.setItem(
                'USER_INFO',
                JSON.stringify({
                    username: account.value.username,
                    password: account.value.password,
                    remmberPsw: true,
                })
            )
        } else {
            localStorage.removeItem('USER_INFO')
        }
    }
    /**
     * 显示账户到表单
     */
    const showUserInfo = () => {
        const userInfo = localStorage.getItem('USER_INFO')
        if (userInfo) {
            const user = JSON.parse(userInfo)
            console.log('正在解密', user.password)
            const password = pwdDecrypt(user.password)
            console.log('解密完成', password)

            account.value.username = user.username
            account.value.password = String(password)
            account.value.remmberPsw = user.remmberPsw
        }
    }
    /**
     * 获取token
     */
    const tokenValue = (pwd, random) => {
        const token = encryptPwd(pwd, random)
        return token
    }
    /**
     * 密码加密
     */
    const pwdEncrypt = text => {
        const pwd = textEncrypt(text)
        return pwd
    }
    /**
     * 密码解密
     */
    const pwdDecrypt = m => {
        const pwd = textDecrypt(m)
        return pwd
    }
    return { account, rules, loginRef, bindLogin }
}
